const axios = require("axios");
const cheerio = require("cheerio");
var path = require("path");
const fs = require("fs");
function k(d, a) {
  var c = (65535 & d) + (65535 & a);
  return (((d >> 16) + (a >> 16) + (c >> 16)) << 16) | (65535 & c);
}
function z(c, a) {
  return (c << a) | (c >>> (32 - a));
}
function M(r, h, m, a, p, d) {
  return k(z(k(k(h, r), k(a, d)), p), m);
}
function E(s, d, e, m, a, p, h) {
  return M((d & e) | (~d & m), s, d, a, p, h);
}
function j(s, d, e, m, a, p, h) {
  return M((d & m) | (e & ~m), s, d, a, p, h);
}
function O(s, d, e, m, a, p, h) {
  return M(d ^ e ^ m, s, d, a, p, h);
}
function L(s, d, e, m, a, p, h) {
  return M(e ^ (d | ~m), s, d, a, p, h);
}
function I(f, c) {
  (f[c >> 5] |= 128 << c % 32), (f[14 + (((c + 64) >>> 9) << 4)] = c);
  var C,
    s,
    R,
    Q,
    t,
    p = 1732584193,
    A = -271733879,
    S = -1732584194,
    o = 271733878;
  for (C = 0; C < f.length; C += 16) {
    (s = p),
      (R = A),
      (Q = S),
      (t = o),
      (A = L(
        (A = L(
          (A = L(
            (A = L(
              (A = O(
                (A = O(
                  (A = O(
                    (A = O(
                      (A = j(
                        (A = j(
                          (A = j(
                            (A = j(
                              (A = E(
                                (A = E(
                                  (A = E(
                                    (A = E(
                                      A,
                                      (S = E(
                                        S,
                                        (o = E(
                                          o,
                                          (p = E(
                                            p,
                                            A,
                                            S,
                                            o,
                                            f[C],
                                            7,
                                            -680876936
                                          )),
                                          A,
                                          S,
                                          f[C + 1],
                                          12,
                                          -389564586
                                        )),
                                        p,
                                        A,
                                        f[C + 2],
                                        17,
                                        606105819
                                      )),
                                      o,
                                      p,
                                      f[C + 3],
                                      22,
                                      -1044525330
                                    )),
                                    (S = E(
                                      S,
                                      (o = E(
                                        o,
                                        (p = E(
                                          p,
                                          A,
                                          S,
                                          o,
                                          f[C + 4],
                                          7,
                                          -176418897
                                        )),
                                        A,
                                        S,
                                        f[C + 5],
                                        12,
                                        1200080426
                                      )),
                                      p,
                                      A,
                                      f[C + 6],
                                      17,
                                      -1473231341
                                    )),
                                    o,
                                    p,
                                    f[C + 7],
                                    22,
                                    -45705983
                                  )),
                                  (S = E(
                                    S,
                                    (o = E(
                                      o,
                                      (p = E(
                                        p,
                                        A,
                                        S,
                                        o,
                                        f[C + 8],
                                        7,
                                        1770035416
                                      )),
                                      A,
                                      S,
                                      f[C + 9],
                                      12,
                                      -1958414417
                                    )),
                                    p,
                                    A,
                                    f[C + 10],
                                    17,
                                    -42063
                                  )),
                                  o,
                                  p,
                                  f[C + 11],
                                  22,
                                  -1990404162
                                )),
                                (S = E(
                                  S,
                                  (o = E(
                                    o,
                                    (p = E(
                                      p,
                                      A,
                                      S,
                                      o,
                                      f[C + 12],
                                      7,
                                      1804603682
                                    )),
                                    A,
                                    S,
                                    f[C + 13],
                                    12,
                                    -40341101
                                  )),
                                  p,
                                  A,
                                  f[C + 14],
                                  17,
                                  -1502002290
                                )),
                                o,
                                p,
                                f[C + 15],
                                22,
                                1236535329
                              )),
                              (S = j(
                                S,
                                (o = j(
                                  o,
                                  (p = j(p, A, S, o, f[C + 1], 5, -165796510)),
                                  A,
                                  S,
                                  f[C + 6],
                                  9,
                                  -1069501632
                                )),
                                p,
                                A,
                                f[C + 11],
                                14,
                                643717713
                              )),
                              o,
                              p,
                              f[C],
                              20,
                              -373897302
                            )),
                            (S = j(
                              S,
                              (o = j(
                                o,
                                (p = j(p, A, S, o, f[C + 5], 5, -701558691)),
                                A,
                                S,
                                f[C + 10],
                                9,
                                38016083
                              )),
                              p,
                              A,
                              f[C + 15],
                              14,
                              -660478335
                            )),
                            o,
                            p,
                            f[C + 4],
                            20,
                            -405537848
                          )),
                          (S = j(
                            S,
                            (o = j(
                              o,
                              (p = j(p, A, S, o, f[C + 9], 5, 568446438)),
                              A,
                              S,
                              f[C + 14],
                              9,
                              -1019803690
                            )),
                            p,
                            A,
                            f[C + 3],
                            14,
                            -187363961
                          )),
                          o,
                          p,
                          f[C + 8],
                          20,
                          1163531501
                        )),
                        (S = j(
                          S,
                          (o = j(
                            o,
                            (p = j(p, A, S, o, f[C + 13], 5, -1444681467)),
                            A,
                            S,
                            f[C + 2],
                            9,
                            -51403784
                          )),
                          p,
                          A,
                          f[C + 7],
                          14,
                          1735328473
                        )),
                        o,
                        p,
                        f[C + 12],
                        20,
                        -1926607734
                      )),
                      (S = O(
                        S,
                        (o = O(
                          o,
                          (p = O(p, A, S, o, f[C + 5], 4, -378558)),
                          A,
                          S,
                          f[C + 8],
                          11,
                          -2022574463
                        )),
                        p,
                        A,
                        f[C + 11],
                        16,
                        1839030562
                      )),
                      o,
                      p,
                      f[C + 14],
                      23,
                      -35309556
                    )),
                    (S = O(
                      S,
                      (o = O(
                        o,
                        (p = O(p, A, S, o, f[C + 1], 4, -1530992060)),
                        A,
                        S,
                        f[C + 4],
                        11,
                        1272893353
                      )),
                      p,
                      A,
                      f[C + 7],
                      16,
                      -155497632
                    )),
                    o,
                    p,
                    f[C + 10],
                    23,
                    -1094730640
                  )),
                  (S = O(
                    S,
                    (o = O(
                      o,
                      (p = O(p, A, S, o, f[C + 13], 4, 681279174)),
                      A,
                      S,
                      f[C],
                      11,
                      -358537222
                    )),
                    p,
                    A,
                    f[C + 3],
                    16,
                    -722521979
                  )),
                  o,
                  p,
                  f[C + 6],
                  23,
                  76029189
                )),
                (S = O(
                  S,
                  (o = O(
                    o,
                    (p = O(p, A, S, o, f[C + 9], 4, -640364487)),
                    A,
                    S,
                    f[C + 12],
                    11,
                    -421815835
                  )),
                  p,
                  A,
                  f[C + 15],
                  16,
                  530742520
                )),
                o,
                p,
                f[C + 2],
                23,
                -995338651
              )),
              (S = L(
                S,
                (o = L(
                  o,
                  (p = L(p, A, S, o, f[C], 6, -198630844)),
                  A,
                  S,
                  f[C + 7],
                  10,
                  1126891415
                )),
                p,
                A,
                f[C + 14],
                15,
                -1416354905
              )),
              o,
              p,
              f[C + 5],
              21,
              -57434055
            )),
            (S = L(
              S,
              (o = L(
                o,
                (p = L(p, A, S, o, f[C + 12], 6, 1700485571)),
                A,
                S,
                f[C + 3],
                10,
                -1894986606
              )),
              p,
              A,
              f[C + 10],
              15,
              -1051523
            )),
            o,
            p,
            f[C + 1],
            21,
            -2054922799
          )),
          (S = L(
            S,
            (o = L(
              o,
              (p = L(p, A, S, o, f[C + 8], 6, 1873313359)),
              A,
              S,
              f[C + 15],
              10,
              -30611744
            )),
            p,
            A,
            f[C + 6],
            15,
            -1560198380
          )),
          o,
          p,
          f[C + 13],
          21,
          1309151649
        )),
        (S = L(
          S,
          (o = L(
            o,
            (p = L(p, A, S, o, f[C + 4], 6, -145523070)),
            A,
            S,
            f[C + 11],
            10,
            -1120210379
          )),
          p,
          A,
          f[C + 2],
          15,
          718787259
        )),
        o,
        p,
        f[C + 9],
        21,
        -343485551
      )),
      (p = k(p, s)),
      (A = k(A, R)),
      (S = k(S, Q)),
      (o = k(o, t));
  }
  return [p, A, S, o];
}
function P(f) {
  var a,
    c = "",
    d = 32 * f.length;
  for (a = 0; a < d; a += 8) {
    c += String.fromCharCode((f[a >> 5] >>> a % 32) & 255);
  }
  return c;
}
function N(f) {
  var a,
    c = [];
  for (c[(f.length >> 2) - 1] = void 0, a = 0; a < c.length; a += 1) {
    c[a] = 0;
  }
  var d = 8 * f.length;
  for (a = 0; a < d; a += 8) {
    c[a >> 5] |= (255 & f.charCodeAt(a / 8)) << a % 32;
  }
  return c;
}
function J(a) {
  return P(I(N(a), 8 * a.length));
}
function H(s, d) {
  var f,
    h,
    m = N(s),
    a = [],
    p = [];
  for (
    a[15] = p[15] = void 0, m.length > 16 && (m = I(m, 8 * s.length)), f = 0;
    f < 16;
    f += 1
  ) {
    (a[f] = 909522486 ^ m[f]), (p[f] = 1549556828 ^ m[f]);
  }
  return (h = I(a.concat(N(d)), 512 + 8 * d.length)), P(I(p.concat(h), 640));
}
function K(f) {
  var a,
    c,
    d = "";
  for (c = 0; c < f.length; c += 1) {
    (a = f.charCodeAt(c)),
      (d +=
        "0123456789abcdef".charAt((a >>> 4) & 15) +
        "0123456789abcdef".charAt(15 & a));
  }
  return d;
}
function b(a) {
  return unescape(encodeURIComponent(a));
}
function G(a) {
  return J(b(a));
}
function D(a) {
  return K(G(a));
}
function q(c, a) {
  return H(b(c), b(a));
}
function w(c, a) {
  return K(q(c, a));
}

function ec(d, a, c) {
  return a ? (c ? q(a, d) : w(a, d)) : c ? G(d) : D(d);
}

function u(a, f, c) {
  var d = "";
  if (a > 0 && f > 0 && c > 0) {
    var n = ec(a);
    var k = ec(f + c);
    var j = n.split("").reverse();
    var e = k.split("");
    for (var b = 0; b < 32; b++) {
      if (b % 2 == 0) {
        d += j[b];
      } else {
        d += e[b];
      }
    }
  }
  return d;
}

function dld(d, s) {
  //f='4474x2517'
  var e = s.split("x"); //e=[4474,2517]
  // var d = g("t").value; //value="1508820807"
  var b = u(parseInt(d), parseInt(e[0]), parseInt(e[1]));
  return b;
}

const instance = axios.create({
  timeout: 60000,
  headers: {
    "user-agent":
      "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
  },
});
let countPic = 1;
const downLoadImg = async (imgObj = {}) => {
  const target_path = path.resolve(
    "C:/Users/Administrator/Desktop/imgList",
    `./${imgObj.title}.jpg`
  );
  const response = await instance.get(imgObj.downUrl, {
    responseType: "stream",
  });

  await response.data.pipe(fs.createWriteStream(target_path));
};

exports.getImgData = async (startIndex) => {
  const baseGetUrl = `https://www.hippopx.com/index.php?c=main&m=portal_loadmore&lang=zh&page=`;
  console.log("开始访问页面,创建跳转Url数组");
  const resData = await instance.get(`${baseGetUrl}${startIndex}`);
  const $ = cheerio.load(resData.data);
  const img_list = [];
  console.log("创建跳转Url数组完成");

  $("li figure a").each((i, element) => {
    img_list.push({
      goUrl: $(element).attr("href"),
      thumbSrc: $(element).find("img").attr("src").slice(0, -9),
      title: $(element).find("img").attr("title"),
    });
  });
  return img_list;
};

exports.goUrlGetD = async (img_list) => {
  for (let i = 0, len = img_list.length; i < len; i++) {
    try {
      console.log("访问带有高清图片的地址", img_list[i].goUrl);

      const resData = await instance.get(img_list[i].goUrl);
      const $ = cheerio.load(resData.data);
      const d = dld($("input#t").attr("value"), $("span#s.info_detail").text());

      console.log(
        "访问完成,得到高清图片地址",
        `${img_list[i].thumbSrc}${d}.jpg`
      );

      const startTime = new Date().getTime();
      console.log(`下载第${countPic}张 ${img_list[i].title}.jpg`);
      console.log("...ing");
      try {
        await downLoadImg({
          ...img_list[i],
          downUrl: `${img_list[i].thumbSrc}${d}.jpg`,
        });
        const endTime = new Date().getTime();
        const totalTime = (endTime - startTime) / 1000;
        console.log(`下载完成，耗时${totalTime}s`);
        console.log("-----------------------------");
        countPic++;
      } catch (e) {
        console.log(`下载第${countPic}张图片失败(${img_list[i].title}.jpg)`);
      }
    } catch (e) {
      console.log("访问含高清图片地址失败", e);
    }
  }
};
